package com.invengo.module_check.ui.fragment.widget;

import android.view.LayoutInflater;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.RecyclerView;

import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.invengo.library_db.entity.module_check.ShelfBean;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.trello.rxlifecycle2.LifecycleProvider;

import me.goldze.mvvmhabit.utils.KLog;

public class SelectShelfDialog {
    public SelectShelfViewModel viewModel;
    private static SelectShelfDialog instance;
    private ViewDataBinding binding;
    private MaterialDialog dialog;

    public static SelectShelfDialog getInstance() {
        if (instance == null) synchronized (SelectShelfDialog.class) {
            if (instance == null) {
                instance = new SelectShelfDialog();
            }
        }
        return instance;
    }

    private SelectShelfDialog() {
    }

    private OnShelfSelectListener listener;
    private FragmentActivity activity;

    public void showDialog(FragmentActivity activity, ShelfBean defaultShelfBean, OnShelfSelectListener listener) {
        this.listener = listener;
        this.defaultBean = defaultShelfBean;
        KLog.e(activity.getClass());
        if (this.activity == null) {
            this.activity = activity;
        }
        if (dialog == null | !activity.equals(this.activity)) {
            this.activity = activity;
            initDialog(activity);
        }
        if (!dialog.isShowing())
            dialog.show();
    }

    private ShelfBean defaultBean;
    MaterialDialog downloadDiaog;

    private void initDialog(FragmentActivity activity) {
        viewModel = ViewModelProviders.of(activity).get(SelectShelfViewModel.class);
        binding = DataBindingUtil.inflate(LayoutInflater.from(activity),
                R.layout.dialog_select_shelf, null, false);
        dialog = new MaterialDialog.Builder(activity)
                .customView(binding.getRoot(), false)
                .showListener(dialog -> {
                    viewModel.loadShelfData(defaultBean);
//                        viewModel.test();
                })
                .dismissListener(dialog -> {
                })
                .build();
        binding.setVariable(BR.viewModel, viewModel);
        binding.setLifecycleOwner(activity);
        //让ViewModel拥有View的生命周期感应
        activity.getLifecycle().addObserver(viewModel);
        //注入RxLifecycle生命周期
        viewModel.injectLifecycleProvider((LifecycleProvider) activity);

        viewModel.uc.downloadDialogEvent.observe(activity, aBoolean -> {
            if (downloadDiaog == null) initDownloadDialog();
            if (aBoolean) {
                downloadDiaog.setProgress(0);
                downloadDiaog.show();
            } else
                downloadDiaog.dismiss();
        });
        viewModel.uc.downloadProgressEvent.observe(activity, integer -> {
            if (downloadDiaog != null && downloadDiaog.isShowing())
                if (integer == -1) {
                    downloadDiaog.setProgress(0);
                } else {
                    downloadDiaog.incrementProgress(integer);
                }
        });
        viewModel.uc.downloadContentEvent.observe(activity, str -> {
            if (downloadDiaog != null && downloadDiaog.isShowing())
                downloadDiaog.setContent(str);
        });
        viewModel.uc.roomMoveEvent.observe(activity, position -> {
            RecyclerView view = binding.getRoot().findViewById(R.id.roomRecycler);
            view.smoothScrollToPosition(position);
        });
        viewModel.uc.rowMoveEvent.observe(activity, position -> {
            RecyclerView view = binding.getRoot().findViewById(R.id.rowRecycler);
            view.smoothScrollToPosition(position);
        });
        viewModel.uc.shelfMoveEvent.observe(activity, position -> {
            RecyclerView view = binding.getRoot().findViewById(R.id.shelfRecycler);
            view.scrollToPosition(position);
        });
        viewModel.uc.closeEvent.observe(activity, bean -> {
            if (listener != null) {
                listener.onSelected(bean);
            }
            dialog.dismiss();
        });

    }


    private void initDownloadDialog() {
        downloadDiaog = new MaterialDialog.Builder(dialog.getContext())
                .title("数据下载中")
                .cancelable(false)
                .content("正在获取图书数据...")
                .contentGravity(GravityEnum.CENTER)
                .progress(false, 100, true)
                .build();
    }
}
